// import dbHelper from '../../../common/dbHelper'
import sqlHelper from './preplanSqlHelper'
// import commonSqlHelper from '../common/commonSqlHelper'
// import store from '../../store'
import SQLite from '../../common/SQLiteHelper'

// const { dll: { UtilDLL } } = require('../../util')
export default {
  async addEvidenceCatalog (dataSource) {
    try {
      console.log('添加示证目录', JSON.parse(dataSource))
      if (process.browser) {
        return await SQLite.ExecuteNonQuery(sqlHelper.addEvidenceCatalog(dataSource))
      } else {
        return await SQLite.ExecuteNonQuery({
          executeSql: sqlHelper.addEvidenceCatalog(dataSource)
        })
      }
    } catch (error) {
      throw error
    }
  },
  // 获取示证大纲目录
  async getEvidenceCatalog () {
    try {
      let dataSource = await SQLite.ExecuteScalar({SQL: sqlHelper.getEvidenceCatalog})
      return dataSource ? JSON.parse(dataSource) : dataSource
    } catch (error) {
      throw error
    }
  },
  // 保存示证大纲目录
  async saveEvidenceCatalog (dataSource) {
    try {
      console.log('更新示证目录', JSON.parse(dataSource))
      let num = await SQLite.ExecuteScalar({
        SQL: `select max( id ) from yx_ya_szml`
      })

      if (num < 5) {
        return await SQLite.ExecuteNonQuery({
          executeSql: sqlHelper.updateEvidenceNode(num + 1, dataSource)
        })
      } else {
        for (let i = 1; i <= num; i++) {
          let data = await SQLite.ExecuteScalar({
            SQL: `SELECT data from yx_ya_szml where id=${i + 1}`
          })
          await SQLite.ExecuteNonQuery({
            executeSql: sqlHelper.updateEvidenceNode(i, data || dataSource)
          })
        }
      }
      return true
    } catch (error) {
      throw error
    }
  },
  async existEvidenceMaterial (param, dbFile) {
    try {
      return await SQLite.ExecuteReader(sqlHelper.existEvidenceMaterial(param), dbFile)
    } catch (error) {
      throw error
    }
  },
  async getEvidenceMaterialText (param) {
    try {
      return await SQLite.ExecuteReader(sqlHelper.getEvidenceMaterialText(param))
    } catch (e) {
      throw e
    }
  }
}
